<?php namespace Admin\Controller;
class GoodsListController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\GoodsList;
	}
	public function index(){
		$tid=Q('get.tid',0,'intval');
		$gid=Q('get.gid',0,'intval');
//		p($gid);
		//获取规格
		$tAttrModel=new \Common\Model\TypeAttr;
		$tData=$tAttrModel->join('goods_attr','beautiful_type_attr.taid','=','beautiful_goods_attr.taid')->where('class=1')->where(array('tid'=>$tid))->where(array('gid'=>$gid))->get();
//		p($tData);
		$abb=array();
		$acc=array();
//		p($tData);
		foreach ($tData as $k => $v) {
//			$acc[$v['taid']]['gtvalue'][]=$v['gtvalue'];
			$abb['spec'][$v['taname']][]=$v['taname'];
			$acc[$v['taid']][$v['gtid']]=$v['gtvalue'];
		}
//			p($abb);p($acc);
//		die;
		View::with('abb',$abb);
		View::with('acc',$acc);
		foreach ($tData as $k => $v) {
			$tData[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		View::with('tData',$tData);
		
		//index的显示
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		$data=$this->model->where(array('gid'=>$gid))->get();
//		p($data);die;
		
		foreach ($data as $k => $v) {
			$data[$k]['combine']=explode(',', $v['combine']);
		}
		foreach ($data as $key => $value) {
//			p($value['combine']);
			foreach ($value['combine'] as $k => $v) {
				$c=$goodsAttrModel->where(array('gtid'=>$v))->pluck('gtvalue');
				$data[$key]['combine'][$k]=$c;
			}
		}
//		p($data);
		View::with('data',$data);
//		p($tData);
//		p($data);
		View::make();
	}
	public function add(){
		$tid=Q('post.tid',0,'intval');
		$gid=Q('post.gid',0,'intval');
//		p($tid);P($gid);die;
		if(IS_POST){
//			p($_POST);die;
			if($this->model->store()){
				View::success('添加成功',U('index',array('gid'=>$gid,'tid'=>$tid)));
			}
			View::error($this->model->getError());
		}
	}
	//删除
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where(array('glid'=>$glid))->delete();
		View::success('删除成功');
	}
	//修改
	public function edit(){
		$tAttrModel=new \Common\Model\TypeAttr;
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		$glid=Q('get.glid',0,'intval');
		$tid=Q('get.tid',0,'intval');
		$gid=Q('get.gid',0,'intval');
		if(IS_POST){
//			p($_POST);die;
			$glids=Q('post.glid',0,'intval');
			$tid=Q('post.tid',0,'intval');
			$gid=Q('post.gid',0,'intval');
//			p($glids);die;
			if($this->model->edit($glids)){
//				die;
				View::success('修改成功',U('index',array('tid'=>$tid,'gid'=>$gid)));
			}
//			die;
			View::error($this->model->getError());
		}
		$tData=$tAttrModel->join('goods_attr','beautiful_type_attr.taid','=','beautiful_goods_attr.taid')->where('class=1')->where(array('tid'=>$tid))->where(array('gid'=>$gid))->get();
		$abb=array();
		$acc=array();
		foreach ($tData as $k => $v) {
			$abb['spec'][$v['taname']][]=$v['taname'];
			$acc[$v['taid']][$v['gtid']]=$v['gtvalue'];
		}
		$oldData=$this->model->where(array('glid'=>$glid))->find();
		

		$oldData['combine']=explode(',', $oldData['combine']);
		foreach ($oldData['combine']as $key => $value) {
			$c=$goodsAttrModel->where(array('gtid'=>$value))->pluck('gtvalue');
			$oldData['combine'][$key]=$c;
		}
		View::with('abb',$abb);
		View::with('acc',$acc);
		View::with('oldData',$oldData);
		View::make();
	}

//	public function gnumber(){
//			$listData=$this->model->get();
//			$goodsModel=new \Common\Model\Goods;
//			$da=array(
//			'gnumber'=>0
//			);
//			
//			foreach ($listData as $k => $v) {
//				$da['gnumber']+=$v['inventory'];
//			}
//			p($da);die;
//			$goodsModel->save($da);
//		}
//	
}



 ?>